﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace HeavenvsDarkness
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class MyPlanarModel : MyModel
    {
        public enum SHAPE
        {
            SQUARE,
            RECT_HORIZONE,
            RECT_VERTICAL
        };

        VertexPositionTexture[] verties;

        public VertexPositionTexture[] Verties
        {
            get { return verties; }
            set { verties = value; }
        }

        Effect myEffect;

        public Effect MyEffect
        {
            get { return myEffect; }
            set { myEffect = value; }
        }
        //A--D
        //B--C



        //A (-1,-1,0) (0,0)
        //B (-1,1,0)  (0,1)
        //C (1,1,0)   (1,1)
        //D (1,-1,0)  (1,0)
        Vector2 padding;

        public Vector2 Padding
        {
            get { return padding; }
            set { padding = value; }
        }
        public MyPlanarModel(float scale, Vector3 position, Matrix rotation, MyPlanarModel.SHAPE shape)
        {
            MyEffect = GameUtil.game.Content.Load<Effect>("Effect1");
            Scale = scale;
            Position = position;
            Rotation = rotation;
           
            Verties = MyPlanarModel.GetVerties(shape);         
        }

        public void Draw(Texture2D texture)
        {

            MyEffect.CurrentTechnique = MyEffect.Techniques["Technique2"];
            MyEffect.Parameters["World"].SetValue(Rotation * Matrix.CreateScale(Scale) * Matrix.CreateTranslation(Position));
            MyEffect.Parameters["View"].SetValue(GameUtil.Camera.View);
            MyEffect.Parameters["Projection"].SetValue(GameUtil.Camera.Projection);
            MyEffect.Parameters["Texture1"].SetValue(texture);
            GameUtil.graphicDevice.BlendState = BlendState.AlphaBlend;


            foreach (EffectPass pass in MyEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GameUtil.graphicDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, Verties, 0, 2, VertexPositionTexture.VertexDeclaration);
            }
        }

        public static VertexPositionTexture[] GetVerties(SHAPE shape)
        {
            VertexPositionTexture[] tempVerties = new VertexPositionTexture[6];
            if (shape == SHAPE.SQUARE)
            {
                tempVerties[0] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1));//A
                tempVerties[1] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0));//B
                tempVerties[2] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0));//C
                tempVerties[3] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1));//A
                tempVerties[4] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0));//C
                tempVerties[5] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1));//D 
                return tempVerties;
            }

            if (shape == SHAPE.RECT_HORIZONE)
            {
                tempVerties[0] = new VertexPositionTexture(new Vector3(-1, -0.25f, 0), new Vector2(0, 1));//A
                tempVerties[1] = new VertexPositionTexture(new Vector3(-1, 0.25f, 0), new Vector2(0, 0));//B
                tempVerties[2] = new VertexPositionTexture(new Vector3(1, 0.25f, 0), new Vector2(1, 0));//C
                tempVerties[3] = new VertexPositionTexture(new Vector3(-1, -0.25f, 0), new Vector2(0, 1));//A
                tempVerties[4] = new VertexPositionTexture(new Vector3(1, 0.25f, 0), new Vector2(1, 0));//C
                tempVerties[5] = new VertexPositionTexture(new Vector3(1, -0.25f, 0), new Vector2(1, 1));//D 
                return tempVerties;
            }

            if (shape == SHAPE.RECT_VERTICAL)
            {
                tempVerties[0] = new VertexPositionTexture(new Vector3(-0.25f, -1, 0), new Vector2(0, 1));//A
                tempVerties[1] = new VertexPositionTexture(new Vector3(-0.25f, 1, 0), new Vector2(0, 0));//B
                tempVerties[2] = new VertexPositionTexture(new Vector3(0.25f, 1, 0), new Vector2(1, 0));//C
                tempVerties[3] = new VertexPositionTexture(new Vector3(-0.25f, -1, 0), new Vector2(0, 1));//A
                tempVerties[4] = new VertexPositionTexture(new Vector3(0.25f, 1, 0), new Vector2(1, 0));//C
                tempVerties[5] = new VertexPositionTexture(new Vector3(0.25f, -1, 0), new Vector2(1, 1));//D 
                return tempVerties;
            }
            return null;
        }

        public static SHAPE GetShape(String str)
        {
            if(str=="Square")
            {
                return SHAPE.SQUARE;
            }

            if (str == "Vertical")
            {
                return SHAPE.RECT_VERTICAL;
            }

            return SHAPE.RECT_HORIZONE;
        }

        
    }
}
